//
//  BSHeaderView.m
//  BS
//
//  Created by lanou on 15/8/14.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSHeaderView.h"

#import "UIImageView+WebCache.h"

#import "BSUserViewController.h"

#import "PlayerManger.h"

@implementation BSHeaderView


//自定义头试图
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"BSHeaderView"owner:self options:nil];
        UIView *nibView = [objects firstObject];
        UIView *contentView = self.contentView;
        CGSize contentViewSize = contentView.frame.size;
        nibView.frame = CGRectMake(0, 0, contentViewSize.width, contentViewSize.height);
        self.profileImageView = (UIImageView *)[nibView viewWithTag:200];
        //头像变圆
        self.profileImageView.layer.cornerRadius = 15;
        self.profileImageView.layer.masksToBounds = YES;
        UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toUser)];
        [self addGestureRecognizer:gest];
        self.nameLabel = (UILabel *)[nibView viewWithTag:201];
        self.passtimeLabel = (UILabel *)[nibView viewWithTag:202];
        self.downLoadBtn = (UIButton *)[nibView viewWithTag:203];
        [self.downLoadBtn addTarget:self action:@selector(downLoad) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:nibView];
    }
    return self;
}

- (void)downLoad{

    if (_model.isDownload) {
        return;
    }
    self.downLoadBtn.selected = YES;
    _model.isDownload = YES;
    if ([_model.type isEqualToString:@"41"]) {
        [[PlayerManger defaultManger] loadWithUrl:_model.videouri];
    }
    if ([_model.type isEqualToString:@"10"]) {
        [[PlayerManger defaultManger] saveImageWithUrl:_model.cdn_img];
    }
}

- (void)toUser{

    BSUserViewController *userVC = [[BSUserViewController alloc] init];
    userVC.uid = _model.user_id;
    [self.nav pushViewController:userVC animated:YES];
}


- (void)setModel:(BSListModel *)model{
    
    _model = model;
    if(_model.isDownload){
        _downLoadBtn.selected = YES;
    }
    else{
        _downLoadBtn.selected = NO;
        
    }
    self.downLoadBtn.hidden = YES;
//    if([_model.type isEqualToString:@"10"]||[_model.type isEqualToString:@"41"]){
//    
//        self.downLoadBtn.hidden = NO;
//    }
//    else{
//        self.downLoadBtn.hidden = YES;
//    }
    _nameLabel.text = model.name;
    _passtimeLabel.text = model.passtime;
    [_profileImageView sd_setImageWithURL:[NSURL URLWithString:model.profile_image]placeholderImage:[UIImage imageNamed:@"iconfont-yonghu"]];
}
@end
